package lumis.service.wsrp.consumer.doui;

import java.util.Date;

import com.sun.portal.wsrp.common.LeaseTime;
import com.sun.portal.wsrp.common.stubs.v2.Lifetime;
import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.service.wsrp.common.WSRPUtils;
import lumis.service.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
import lumis.service.wsrp.consumer.producermanager.ProducerEntityManagerImpl;
import lumis.util.ITransaction;

/**
 * Reads producer and populate a {@link TabularData} with its data.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ProducerDataProvider implements IDataProvider<TabularSource<ITabularSourceField>>
{
	public void loadData(SessionConfig sessionConfig,
						TabularSource<ITabularSourceField> source,
						ITransaction transaction)
				throws PortalException
	{
		Date dateLifetime = obtainLifetime(source);
		
		TabularData tabularData = source.getData();
		fillTabularData(tabularData, dateLifetime);
	}
	
	/**
	 * Retrieves the lifetime.
	 * @param source	the source.
	 * @return			an instance of Date.
	 * @throws UnexpectedException
	 */
	private Date obtainLifetime(
							TabularSource<ITabularSourceField> source)
						throws UnexpectedException
	{
		// check if current service instance is configured
		String serviceInstanceId = source.getDouiContext().getRequest().getServiceInstanceConfig().getId();
		ProducerEntityManager producerEntityManager = ProducerEntityManagerFactory.getProducerEntityManager(serviceInstanceId);
		ProducerEntity producerEntity = null;
		
		try
		{
			Date dateLifetime = null;
			
			// get producer entity
			if (!(producerEntityManager.getProducerEntityIds().isEmpty()))
			{
				producerEntity = producerEntityManager.getProducerEntity(producerEntityManager.getProducerEntityIds().iterator().next());
				
				// get lifetime			
				
				if ((producerEntity.getRegistrationContext() != null) 
						&& (producerEntity.getRegistrationContext().getRegistrationHandle() != null))
				{
					LeaseTime lifetime = producerEntity.getLifetime();
					if (lifetime != null)
					{
						dateLifetime = WSRPUtils.dateFromXMLGregorianCalendar(lifetime.getTerminationTime());
					}
				}
			}			
			
			return dateLifetime;
		}
		catch (WSRPConsumerException e)
		{
			// throw new UnexpectedException(e);
			return null;
		}
	}
	
	/**
	 * Fills tabular data with producer entity data.
	 * @param producerEntity	the ProducerEntity instance.
	 * @param tabularData		the tabular data.
	 */
	private void fillTabularData(
					TabularData tabularData,
					Date lifetime)
	{
		ISourceData row = tabularData.addRow();
		row.put("lifetime", lifetime);
	}
}